#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>


#define OUTPUT_PATH "launch_app.reg"
#define INPUT_PATH "config.list"

// 添加双引号
char* add_quotes(const char *str) {
    size_t len = strlen(str);
    char *quoted_str = (char *)malloc(len + 3);  // +3 for two quotes and null terminator
    if (!quoted_str) {
        return NULL;
    }
    snprintf(quoted_str, len + 3, "\"%s\"", str);
    return quoted_str;
}

// 获取应用程序名称
char* get_appname(const char *buffer) {
    // 找到最后一个反斜杠的位置
    char *programName = strrchr(buffer, '\\');
    if (!programName) return NULL;

    // 移除最后一个反斜杠后的部分并去除双引号
    char *app_name = strdup(programName + 1);
    if (!app_name) return NULL;

    // 去除开头和结尾的双引号
    size_t len = strlen(app_name);
    if (len > 0 && app_name[0] == '"') {
        memmove(app_name, app_name + 1, len - 1); // 移动字符串，去掉第一个字符
        app_name[len - 1] = '\0'; // 结束符
    }
    len = strlen(app_name);
    if (len > 0 && app_name[len - 1] == '"') {
        app_name[len - 1] = '\0'; // 去掉最后一个字符
    }

    return app_name;
}


// 转义反斜杠
char* escape_backslashes(const char *str) {
    size_t len = strlen(str);
    size_t num_backslashes = 0;

    for (const char *ptr = str; *ptr; ptr++) {
        if (*ptr == '\\') {
            num_backslashes++;
        }
    }

    char *escaped_str = (char*)malloc(len + num_backslashes + 1);
    if (!escaped_str) return NULL;

    char *dest = escaped_str;
    for (const char *ptr = str; *ptr; ptr++) {
        if (*ptr == '\\') {
            *dest++ = '\\';
        }
        *dest++ = *ptr;
    }
    *dest = '\0';

    return escaped_str;
}

// 转义双引号
char* escape_quotes(const char *str) {
    size_t len = strlen(str);
    size_t num_quotes = 0;

    for (const char *ptr = str; *ptr; ptr++) {
        if (*ptr == '"') {
            num_quotes++;
        }
    }

    char *escaped_str = (char*)malloc(len + num_quotes + 1);
    if (!escaped_str) return NULL;

    char *dest = escaped_str;
    for (const char *ptr = str; *ptr; ptr++) {
        if (*ptr == '"') {
            *dest++ = '\\';
        }
        *dest++ = *ptr;
    }
    *dest = '\0';

    return escaped_str;
}
int input_integer() {
    int num;
    printf("请输入一个整数: ");
    while (scanf("%d", &num) != 1) { // 检查输入是否有效
        printf("无效输入，请重新输入一个整数: ");
        while (getchar() != '\n'); // 清空输入缓冲区
    }
    return num;
}
int main() {
    SetConsoleOutputCP(65001);  // 设置控制台输出为 UTF-8 编码
    int option=0;
    printf("[1].将列表的软件配置为自启动\r\n");
    printf("[2].将列表的软件从自启动中删除\r\n");
    printf("[注意].此方式只是通过注册表操作的\r\n");
    option =input_integer();
  

    



    char buffer[256] = "";
    const char *str = "-startup";

    FILE *input_file = fopen(INPUT_PATH, "r");
    FILE *output_file = fopen(OUTPUT_PATH, "w");

    if (!input_file || !output_file) {
        printf("无法打开文件！\n");
        return 1;
    }

    fprintf(output_file, "Windows Registry Editor Version 5.00\n");
    fprintf(output_file, "\n");
    fprintf(output_file, "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run]\n");


   

    

    while (fgets(buffer, sizeof(buffer), input_file)) {
        buffer[strcspn(buffer, "\r\n")] = 0; // 去掉换行符

        // 处理键名
        char *key_name = get_appname(buffer);
        if (!key_name) continue; // 错误处理

        char *quoted_key_name = add_quotes(key_name);
        free(key_name);
        if (!quoted_key_name) continue; // 错误处理

        // 处理键值
        char *escaped_key_value = escape_backslashes(buffer);
        if (!escaped_key_value) {
            free(quoted_key_name);
            continue; // 错误处理
        }

        char *escaped_value_with_quotes = escape_quotes(escaped_key_value);
        free(escaped_key_value);
        if (!escaped_value_with_quotes) {
            free(quoted_key_name);
            continue; // 错误处理
        }

        size_t value_len = strlen(escaped_value_with_quotes) + strlen(str) + 1;
        char *key_value_with_args = (char*)malloc(value_len + 1);
        if (!key_value_with_args) {
            free(quoted_key_name);
            free(escaped_value_with_quotes);
            continue; // 错误处理
        }
        snprintf(key_value_with_args, value_len + 1, "%s%s", escaped_value_with_quotes, str);

        
        free(escaped_value_with_quotes);

        char *final_key_value = add_quotes(key_value_with_args);
        free(key_value_with_args);


        // 输出到文件
        if(option==1)
        {
            fprintf(output_file, "%s=%s\n", quoted_key_name, final_key_value);

        }
        else if(option==2)
        {
            fprintf(output_file, "%s=-\n", quoted_key_name);

        }
        else
        {
            printf("将退出程序\r\n");
            Sleep(1000);
            exit (0);

        }

        if (!final_key_value) {
            free(quoted_key_name);
            continue; // 错误处理
        }
     
         
        
        
        

        free(quoted_key_name);
        free(final_key_value);
    }

    fclose(input_file);
    fclose(output_file);

    printf("按任意键继续...");
    getchar();

    return 0;
}
